<?php

	Yii::import('application.vendors.*');// nnhap 1 file tu vender vao
	require_once ('guzzle/guzzle.phar');
	use Guzzle\Http\Client;
	
	class EzRestApi
	{
		var $base_url = 'http://localhost/yii-blog-rest/index.php/';
		//var $base_url = 'http://rest.ezstore.vn/rest/';
      
                
		var $url_login = 'authentication/{username}/{password}';





		var $url_get_favourites = 'web/accounts/{accountId}/locations/{locationId}/favourites';
		var $url_get_favourite_products = 'web/accounts/{accountId}/locations/{locationId}/favourites/{favouriteId}/products';
		var $url_get_product = 'web/accounts/{accountId}/locations/{locationId}/products/{productId}';
		var $url_search_customer = 'web/accounts/{accountId}/locations/{locationId}/customers?s={keyword}';
		var $url_save_order = 'web/accounts/{accountId}/locations/{locationId}/orders/save';
		var $url_register_customer = 'web/accounts/{accountId}/locations/{locationId}/customers/register';
		var $url_get_customer_order= 'web/accounts/{accountId}/locations/{locationId}/customers/{customerId}/orders';
		var $url_get_customer_address= 'web/accounts/{accountId}/locations/{locationId}/customers/{customerId}/customerContacts';
		var $url_delete_customer_contact = 'web/accounts/{accountId}/locations/{locationId}/customerContacts/{customerContactId}/delete';
		var $url_api_search_invoice = 'api/accounts/{accountId}/locations/{locationId}/invoices?s={keyword}&fromDate={fromDate}&toDate={toDate}&pageNumber={pageNumber}&pageSize={pageSize}';
		var $url_api_search_order = 'api/accounts/{accountId}/locations/{locationId}/orders?s={keyword}&fromDate={fromDate}&toDate={toDate}&pageNumber={pageNumber}&pageSize={pageSize}';
		var $url_api_search_customer = 'api/accounts/{accountId}/locations/{locationId}/customers?s={keyword}&pageNumber={pageNumber}&pageSize={pageSize}';
		var $url_api_search_product = 'api/accounts/{accountId}/locations/{locationId}/products?s={keyword}&pageNumber={pageNumber}&pageSize={pageSize}';
		var $url_api_summaryPurchase = 'api/accounts/{accountId}/locations/{locationId}/summaryPurchase?period=2';
		var $url_api_summary = 'api/accounts/{accountId}/locations/{locationId}/summary?fromDate={fromDate}&toDate={toDate}';
		var $url_api_summary_detail = 'api/accounts/{accountId}/locations/{locationId}/summaryDetail?fromDate={fromDate}&toDate={toDate}';
		var $url_api_get_location = 'api/accounts/{accountId}/locations/{locationId}';
		var $url_api_get_invoice = 'api/accounts/{accountId}/locations/{locationId}/invoices/{invoiceId}';
		var $url_api_get_order = 'api/accounts/{accountId}/locations/{locationId}/orders/{orderId}';
		var $url_api_get_product = 'api/accounts/{accountId}/locations/{locationId}/products/{productId}';
                
                
		var $url_api_save_product = 'api/accounts/{accountId}/locations/{locationId}/products/save';
                
                var $url_api_save_image_product='api/accounts/{accountId}/locations/{locationId}/products/{productId}/saveImage';
                
                var $url_api_delete_image_product='api/accounts/{accountId}/locations/{locationId}/products/{productId}/deleteImage';
                
                
		var $url_api_save_customer = 'api/accounts/{accountId}/locations/{locationId}/customers/save';
		var $url_api_get_product_image = 'api/accounts/%d/locations/%d/products/%d/defaultImage';
		var $url_api_get_customer = 'api/accounts/{accountId}/locations/{locationId}/customers/{customerId}';
		var $url_api_get_customer_image = 'api/accounts/%d/locations/%d/customers/%d/image';
		var $url_api_get_product_categories = 'api/accounts/{accountId}/locations/{locationId}/productCategories';
		var $url_api_get_product_units = 'api/accounts/{accountId}/locations/{locationId}/productUnits';
		var $url_api_get_customer_categories = 'api/accounts/{accountId}/locations/{locationId}/customerCategories';
		
                var $url_api_lst_shop='api/accounts/{accountId}/users/{userId}/locations';
                
                
		function EzRestApi() 
		{
			
		}
		
		function getData($url)
		{
			$context = stream_context_create(array(
			    'http' => array(
			        'header'  => "Authorization: Basic " . base64_encode("web:513ff453#9G!%0kjh2454h8jndskb34434")
			    )
			));
			$data = file_get_contents($url, false, $context);
			return $data;
		}
		
		function getResponse($url, $params)
		{
			$client=new Client($url,$params);
			$request=$client->get('')->setAuth('web', '513ff453#9G!%0kjh2454h8jndskb34434');
			$response=$request->send();
			//print $response;			
			$result=$response->json();
			return $result;
		}

		function getRawResponse($url, $params)
		{
			$client=new Client($url,$params);
			$request=$client->get('')->setAuth('web', '513ff453#9G!%0kjh2454h8jndskb34434');
			$response=$request->send();
			//print ($response->getBody());
			//var_dump($response);
			return $response->getBody();
		}
		
		function getDeleteResponse($url, $params)
		{
			$client=new Client($url,$params);
			$request=$client->delete('')->setAuth('web', '513ff453#9G!%0kjh2454h8jndskb34434');
			$response=$request->send();			
			$result=$response->json();
			return $result;
		}
		
		function getPostResponse($url, $params,$body)
		{   
			$client=new Client($url,$params);
			$request = $client->post('',null,$body)->setAuth('web', '513ff453#9G!%0kjh2454h8jndskb34434');
			$request->addHeader('Content-Type', 'application/xml');
			
			$response=$request->send();					
			$result=$response->json();
			return $result;
		}

		function login($username,$password)
		{
			$client = new Client($this->base_url.$this->url_login,array('username'=>$username,'password'=>
			$password));

			if ($client == true) 
			{
				echo 'đang nhap thanh cong ';
			}
			elseif ($client == false) 
			{
				echo 'đang nhap sai';
			}
			
			//$request=$client->get('')->setAuth($accountNo.'/'.$username, $password);
			//$response=$request->send();						
			//$result=$response->json();
			//return $result;
		}
		
		
		
		function searchInvoice($accountId, $locationId,$keyword,$fromDate,$toDate,$page= 0, $size=50)
		{
			$fromDateStr = "";
			if (is_string($fromDate))
			{
				$fromDateStr = $fromDate;
			}
			else if ($fromDate != null)
			{
				$fromDateStr = date_format($fromDate,"Ymd");
			}	
			$toDateStr = "";
			if (is_string($toDate))
			{
				$toDateStr = $toDate;
			}
			else if ($toDate != null)
			{
				$toDateStr = date_format($toDate,"Ymd");
			}			
			return $this->getResponse($this->base_url.$this->url_api_search_invoice,array('accountId'=>
			$accountId,'locationId'=>$locationId,'keyword'=>$keyword, 'fromDate'=>$fromDateStr, 'toDate'=>$toDateStr,'pageSize'=>$size,'pageNumber'=>$page));
		}
		
		function searchOrder($accountId, $locationId,$keyword,$fromDate,$toDate,$page= 0, $size=50)
		{			
			$fromDateStr = "";
			if (is_string($fromDate))
			{
				$fromDateStr = $fromDate;
			}
			else if ($fromDate != null)
			{
				$fromDateStr = date_format($fromDate,"Ymd");
			}	
			$toDateStr = "";
			if (is_string($toDate))
			{
				$toDateStr = $toDate;
			}
			else if ($toDate != null)
			{
				$toDateStr = date_format($toDate,"Ymd");
			}			
			return $this->getResponse($this->base_url.$this->url_api_search_order,array('accountId'=>
			$accountId,'locationId'=>$locationId,'keyword'=>$keyword, 'fromDate'=>$fromDateStr, 'toDate'=>$toDateStr,'pageSize'=>$size,'pageNumber'=>$page));
		
		}
		
		function getOrder($accountId, $locationId, $orderId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_order,array('accountId'=>
			$accountId,'locationId'=>$locationId,'orderId'=>$orderId));
		}
		
		function searchProduct($accountId, $locationId,$keyword,$page= 0, $size=50)
		{			
			return $this->getResponse($this->base_url.$this->url_api_search_product,array('accountId'=>
			$accountId,'locationId'=>$locationId,'keyword'=>$keyword,'pageSize'=>$size,'pageNumber'=>$page));
		}
		
		function searchCustomer($accountId, $locationId,$keyword,$page= 0, $size=50)
		{			
			return $this->getResponse($this->base_url.$this->url_api_search_customer,array('accountId'=>
			$accountId,'locationId'=>$locationId,'keyword'=>$keyword,'pageSize'=>$size,'pageNumber'=>$page));
		}
		
		function getSummaryPurchase($accountId, $locationId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_summaryPurchase,array('accountId'=>
			$accountId,'locationId'=>$locationId));
		}

		function getSummary($accountId, $locationId,$fromDate,$toDate)
		{
			$fromDateStr = "";
			if (is_string($fromDate))
			{
				$fromDateStr = $fromDate;
			}
			else if ($fromDate != null)
			{
				$fromDateStr = date_format($fromDate,"Ymd");
			}	
			$toDateStr = "";
			if (is_string($toDate))
			{
				$toDateStr = $toDate;
			}
			else if ($toDate != null)
			{
				$toDateStr = date_format($toDate,"Ymd");
			}				
			return $this->getResponse($this->base_url.$this->url_api_summary,array('accountId'=>
			$accountId,'locationId'=>$locationId, 'fromDate'=>$fromDateStr, 'toDate'=>$toDateStr));
		}
		
				
		function getSummaryWithDetail($accountId, $locationId,$fromDate,$toDate)
		{			
			return $this->getResponse($this->base_url.$this->url_api_summary_detail,array('accountId'=>
			$accountId,'locationId'=>$locationId, 'fromDate'=>date_format($fromDate,"Ymd"), 'toDate'=>date_format($toDate,"Ymd")));
		}
		
		function getLocation($accountId, $locationId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_location,array('accountId'=>
			$accountId,'locationId'=>$locationId));
		}
		function getInvoice($accountId, $locationId, $invoiceId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_invoice,array('accountId'=>
			$accountId,'locationId'=>$locationId,'invoiceId'=>$invoiceId));
		}
		
		function getInvoiceResponse($accountId, $locationId, $invoiceId)
		{			
			return $this->getRawResponse($this->base_url.$this->url_api_get_invoice,array('accountId'=>
			$accountId,'locationId'=>$locationId,'invoiceId'=>$invoiceId));
		}
		
		function getProduct($accountId, $locationId, $productId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_product,array('accountId'=>
			$accountId,'locationId'=>$locationId,'productId'=>$productId));
		}
		
		function getProductResponse($accountId, $locationId, $productId)
		{			
			return $this->getRawResponse($this->base_url.$this->url_api_get_product,array('accountId'=>
			$accountId,'locationId'=>$locationId,'productId'=>$productId));
		}

		function getProductCategories($accountId, $locationId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_product_categories,array('accountId'=>
			$accountId,'locationId'=>$locationId));
		}
		
		function getProductUnits($accountId, $locationId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_product_units,array('accountId'=>
			$accountId,'locationId'=>$locationId));
		}
		
		function getCustomer($accountId, $locationId, $customerId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_customer,array('accountId'=>
			$accountId,'locationId'=>$locationId,'customerId'=>$customerId));
		}

		function getCustomerCategories($accountId, $locationId)
		{			
			return $this->getResponse($this->base_url.$this->url_api_get_customer_categories,array('accountId'=>
			$accountId,'locationId'=>$locationId));
		}
		
		function getCustomerResponse($accountId, $locationId, $customerId)
		{			
			return $this->getRawResponse($this->base_url.$this->url_api_get_customer,array('accountId'=>
			$accountId,'locationId'=>$locationId,'customerId'=>$customerId));
		}
		
		function getProductImage($accountId, $locationId, $productId)
		{
			$imageUrl = sprintf($this->base_url.$this->url_api_get_product_image, $accountId, $locationId,$productId);
			return $this->getData($imageUrl);
		}
		
		function getCustomerImage($accountId, $locationId, $customerId)
		{
			$imageUrl = sprintf($this->base_url.$this->url_api_get_customer_image, $accountId, $locationId,$customerId);
			return $this->getData($imageUrl);
		}
		function saveImageProduct($accountId, $locationId, $productId,$productImage)
		{
                    //echo "acc :"+$accountId+"location :"+$locationId+"produtc id:"+$productId
                   echo $productImage;

//			$product = new Product();
//			$product->AccountId = $accountId;
//			$product->LocationId = $locationId;
//			$product->ProductId = $productId;
//			//$product->ProductImage=$ProductImage;
//			$xmlSerializer = new XMLSerializer();
//			$productXml= $xmlSerializer->generateValidXmlFromObj($product,'Product');
//                        
//			return $this->getPostResponse($this->base_url.$this->url_api_save_image_product,array('accountId'=>
//			$accountId,'locationId'=>$locationId,'productId'=>$productId),$productXml);
		}
                function deleteImageProduct($accountId, $locationId, $productId)
		{
                        //echo "test delete image ";
                    
			$product = new Product();
			$product->AccountId = $accountId;
			$product->LocationId = $locationId;
			$product->ProductId = $productId;
			
			$xmlSerializer = new XMLSerializer();
			$productXml= $xmlSerializer->generateValidXmlFromObj($product,'Product');
                        
			return $this->getDeleteResponse($this->base_url.$this->url_api_delete_image_product,array('accountId'=>
			$accountId,'locationId'=>$locationId,'productId'=>$productId));			
                        
		}
		function saveProduct($accountId, $locationId, $productId, $productCode, $productName, $price, $categoryId, $unitId, $description)
		{
			$product = new Product();
			$product->AccountId = $accountId;
			$product->LocationId = $locationId;
			$product->ProductId = $productId;
			$product->ProductCode = $productCode;
			$product->ProductName = $productName;
			$product->Price = $price;
			$product->Description = $description;
			$product->DefaultProductCategoryId = $categoryId;
			$product->DefaultProductUnitId = $unitId;
			$xmlSerializer = new XMLSerializer();
			$productXml= $xmlSerializer->generateValidXmlFromObj($product,'Product');
			return $this->getPostResponse($this->base_url.$this->url_api_save_product,array('accountId'=>
			$accountId,'locationId'=>$locationId),$productXml);			
		}
		
		function saveCustomer($accountId, $locationId, $customerId ,$customerCode, $customerFullName,
								$customerCategoryId, $phone, $mobile, $email, $address, $description)
		{
			$customer = new Customer();
			$customer->AccountId = $accountId;
			$customer->LocationId = $locationId;
			$customer->CustomerId = $customerId;
			$customer->CustomerCode = $customerCode;
			$customer->CustomerFullName = $customerFullName;
			$customer->CustomerCategoryId = $customerCategoryId;
			$customer->Phone = $phone;
			$customer->Mobile = $mobile;
			$customer->Address = $address;
			$customer->Email = $email;
			$xmlSerializer = new XMLSerializer();
			$customerXml= $xmlSerializer->generateValidXmlFromObj($customer,'Customer');
			
			$response = $this->getPostResponse($this->base_url.$this->url_api_save_customer,array('accountId'=>
			$accountId,'locationId'=>$locationId),$customerXml);
			
			return $response;	
		}
                
                function getLstshop($accountId, $userId)
		{	
		 	return $this->getResponse($this->base_url.$this->url_api_lst_shop,array('accountId'=>
		 	$accountId,'userId'=>$userId));
		}
	}

	class Customer extends stdClass
	{		
		public $AccountId;
		public $LocationId;
		public $AccountNo;
		public $CustomerId;
		public $CustomerCode;
		public $CustomerFullName;
		public $CustomerCategoryId;
		public $Address;
		public $Phone;
		public $Mobile;
		public $Email;
		public $Description;
	}
	
	class Product extends stdClass
	{		
		public $AccountId;
		public $LocationId;
		public $AccountNo;
		public $ProductId;
		public $ProductCode;
		public $ProductName;
		public $DefaultProductCategoryId;
		public $DefaultProductUnitId;
		public $Price;
		public $Description;	
                public $ProductImage;
	}

?>
